import { message } from 'antd';
import {
  addUserGroup,
  updateUserGroup,
  getUserGroupList,
  getEmployeeOfGroup,
  delEmployeeOfGroup,
  enableGroup,
  disableGroup,
  deleteGroup,
  getEmployeeOfOrg,
  getEmployeeAndOrgList,
} from '../services';
import { getRoleList } from '../../commonrolemanage/services';

export default {
  namespace: 'userGroupManageModel',
  state: {
    userGroupList: [],
    userGroupListTotal: 0,
  },
  effects: {
    *getUserGroupList({ payload }, { put, call, select }) {
      const res = yield call(getUserGroupList, payload);
      const { code, data, message: errMsg } = res;
      if (code === 0) {
        const { dataList, total } = data;
        yield put({
          type: 'updateModel',
          payload: {
            userGroupList: dataList,
            userGroupListTotal: total,
          },
        });
      } else {
        message.error(errMsg);
      }
    },
    *getRoleSelectList({ payload }, { put, call, select }) {
      return yield call(getRoleList, payload);
    },
    *addUserGroup({ payload }, { put, call, select }) {
      return yield call(addUserGroup, payload);
    },
    *updateUserGroup({ payload }, { put, call, select }) {
      return yield call(updateUserGroup, payload);
    },
    *getEmployeeOfGroup({ payload }, { put, call, select }) {
      return yield call(getEmployeeOfGroup, payload);
    },
    *delEmployeeOfGroup({ payload }, { put, call, select }) {
      return yield call(delEmployeeOfGroup, payload);
    },
    *enableGroup({ payload }, { put, call, select }) {
      return yield call(enableGroup, payload);
    },
    *disableGroup({ payload }, { put, call, select }) {
      return yield call(disableGroup, payload);
    },
    *deleteGroup({ payload }, { put, call, select }) {
      return yield call(deleteGroup, payload);
    },
    *getEmployeeOfOrg({ payload }, { put, call, select }) {
      return yield call(getEmployeeOfOrg, payload);
    },
    *getEmployeeAndOrgList({ payload }, { put, call, select }) {
      return yield call(getEmployeeAndOrgList, payload);
    },
  },
  reducers: {
    updateModel: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
};
